cmake_minimum_required(VERSION 3.5)
# Set Project Name
project(kylin-printer-applet LANGUAGES C CXX)

if(CMAKE_VERSION VERSION_LESS "3.7.0")
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
find_package(PkgConfig REQUIRED)
pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
pkg_check_modules(KYSDKKABASE_PKG kysdk-kabase)
pkg_check_modules(KYSDKQTWIDGETS_PKG kysdk-qtwidgets)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets LinguistTools X11Extras Network DBus REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets LinguistTools X11Extras Network DBus REQUIRED)
find_package(KF5WindowSystem REQUIRED)

#Gsetting
find_package(PkgConfig REQUIRED)
pkg_check_modules(QGSETTINGS REQUIRED IMPORTED_TARGET gsettings-qt)

set (SOURCE_PATH ..)

# add ukui log
file(GLOB_RECURSE UKUI_LOG4QT "/usr/lib/libukui-log4qt.so")

#add include 
include_directories(${SOURCE_PATH}/backend)
include_directories(${SOURCE_PATH}/backend/ui)
include_directories(${SOURCE_PATH}/printer_manager/)
include_directories(${SOURCE_PATH}/device_manager/)
include_directories(${SOURCE_PATH}/ui/)
include_directories(${SOURCE_PATH}/ui/main_win_ui/)
include_directories(${SOURCE_PATH}/util)
include_directories(${SOURCE_PATH}/dictionary)
include_directories(${SOURCE_PATH}/common)
include_directories(${SOURCE_PATH}/ui/custom_ui)
include_directories(${SOURCE_PATH}/ui/new_property_window/)
include_directories(
    ${SOURCE_PATH}/avahi_qt/
    ${SOURCE_PATH}/qt_zeroconf/
)

add_definitions("-Wall")

file(GLOB common_SRC 
    "${SOURCE_PATH}/common/*.cpp"
    "${SOURCE_PATH}/common/*.h"
    )

file(GLOB avahi_qt_SRC 
    "${SOURCE_PATH}/avahi_qt/*.cpp"
    "${SOURCE_PATH}/avahi_qt/*.h"
    "${SOURCE_PATH}/qt_zeroconf/*.cpp"
    "${SOURCE_PATH}/qt_zeroconf/*.h"
    )

file(GLOB printer_manager_SRC 
    "${SOURCE_PATH}/printer_manager/*.cpp"
    "${SOURCE_PATH}/printer_manager/*.c"
    "${SOURCE_PATH}/printer_manager/*.h"
    )

file(GLOB device_manager_SRC 
    "${SOURCE_PATH}/device_manager/device_information.*"
    "${SOURCE_PATH}/device_manager/device_map.*"
    "${SOURCE_PATH}/device_manager/device_monitor.*"
    "${SOURCE_PATH}/device_manager/network_device_detector.*"
    "${SOURCE_PATH}/device_manager/network_device_monitor.*"
    "${SOURCE_PATH}/device_manager/udev_devices.*"
    )

file(GLOB dictionary_SRC
    "${SOURCE_PATH}/dictionary/*.cpp"
    "${SOURCE_PATH}/dictionary/*.h"
)

file(GLOB ui_SRC 
    "${SOURCE_PATH}/ui/*.cpp"
    "${SOURCE_PATH}/ui/*.h"
    "${SOURCE_PATH}/ui/custom_ui/*"
    "${SOURCE_PATH}/ui/main_win_ui/*.cpp"
    "${SOURCE_PATH}/ui/main_win_ui/*.h"
    "${SOURCE_PATH}/ui/new_property_window/*.h"
    "${SOURCE_PATH}/ui/new_property_window/*.cpp"
    )

file(GLOB util_SRC
    "${SOURCE_PATH}/util/*.cpp"
    "${SOURCE_PATH}/util/*.h"
    )

file(GLOB ${PROJECT_NAME}_SRC
    "main.cpp"
    "launch_printer.*"
    "ui/tray_win.*"
    "event_notify_monitor.*"
)
SET(ui_RESOURCE ${SOURCE_PATH}/ui/picture.qrc)

QT5_CREATE_TRANSLATION(QM_FILES
    ${ui_SRC}
    ${TS_FILES})

# message(${test_networkdevicebrowse_SRC})
add_executable(${PROJECT_NAME}
    ${${PROJECT_NAME}_SRC}
    ${common_SRC}
    ${printer_manager_SRC}
    ${device_manager_SRC}
    ${ui_SRC}
    ${util_SRC}
    ${ui_RESOURCE}
    ${avahi_qt_SRC}
    ${dictionary_SRC}
    ${QM_FILES}
)

target_link_libraries(${PROJECT_NAME} ${UKUI_LOG4QT})

target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${KYSDKQTWIDGETS_PKG_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKKABASE_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKKABASE_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME}  ${KYSDKKABASE_PKG_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${KYSDKQTWIDGETS_PKG_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} Qt5::Widgets ${KYSDKQTWIDGETS_PKG_LIBRARIES})

target_link_libraries(${PROJECT_NAME} Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::X11Extras Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::DBus)

#dependencies
target_link_libraries(${PROJECT_NAME} "-lpthread -lz")
#cups
target_link_libraries(${PROJECT_NAME} "-lcups")
#cupsfilter
target_link_libraries(${PROJECT_NAME} "-lcupsfilters")
#deb install
target_link_libraries(${PROJECT_NAME} KF5::WindowSystem)

# avahi
target_link_libraries(${PROJECT_NAME} "-lavahi-common -lavahi-client")

# snmp
find_library(NETSNMPAGENT "netsnmpagent")
find_library(NETSNMPMIBS "netsnmpmibs")
find_library(NETSNMP "netsnmp")

# -lusb-1.0
target_link_libraries(${PROJECT_NAME} "-lusb-1.0")
target_link_libraries(${PROJECT_NAME} "-ludev")
target_link_libraries(${PROJECT_NAME} "-lglib-2.0")

target_link_libraries(${PROJECT_NAME} ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP})

target_link_libraries(${PROJECT_NAME} -lX11)
target_link_libraries(${PROJECT_NAME} ${UKUI_LOG4QT})
target_link_libraries(${PROJECT_NAME} PkgConfig::QGSETTINGS)

install(TARGETS kylin-printer-applet DESTINATION /usr/bin/)
#install(FILES ${PROJECT_SOURCE_DIR}/data/kylin-printer-applet.desktop DESTINATION /etc/xdg/autostart/)
install(FILES ${PROJECT_SOURCE_DIR}/data/org.ukui.log4qt.kylin-printer-applet.gschema.xml DESTINATION /usr/share/glib-2.0/schemas/)
